<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;
use App\ShopMaster;
use App\Shop;
use Input;
use DB;
class ShopMasterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //此处暂不作用
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //获取列表
        $datas = ShopMaster::where('shopid',$id)->get();
        //获取shop信息
        $shopname = Shop::where('shopid',$id)->value('shopname');
        return view('admin.shopmaster.index',compact('datas','shopname'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获取数据
       $datas =  ShopMaster::where('hmid',$id)->first();
       return view('admin.shopmaster.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //首先进行验证
        $this -> validate($request,[
            'nickname' => 'required|max:32',
            'referral' => 'required|max:255',
        ],[
            'nickname.required'=>'微信昵称不能为空',
            'nickname.max'=>'微信昵称过长',
            'referral.required'=>'简介不能为空',
            'referral.max'=>'简介过长'
        ]);
        $privilege = Input::get('privilege');
        $nickname = Input::get('nickname');
        $referral = Input::get('referral');
        $subscribe = Input::get('subscribe');
        $ismanage = Input::get('ismanage');
        $asstatus = Input::get('asstatus');
        $headpic = Input::get('headpic');
        //先判断是否有文件
        if(!($request -> hasFile('file'))){
            //直接更新数据库
            $res = ShopMaster::where('hmid',$id)->update(['privilege'=>$privilege,'nickname'=>$nickname,'referral'=>$referral,'subscribe'=>$subscribe,'ismanage'=>$ismanage,'asstatus'=>$asstatus]);
            if($res)
            return back() -> with(['info'=>'数据更新成功']);
            return back() -> with(['info'=>'数据更新失败']);
        }
        if($request -> hasFile('file')){
            //说明有头像信息传来
            $file = Input::file('file');
            $extension = strtolower($file -> getClientOriginalExtension());
            if(!in_array($extension,['png','jpg']))
            return back() -> with(['info'=>'需要ping类型或者jpg类型']);
            //首先获取扩展名
            $clientName = $file -> getClientOriginalName();
            $disk = QiniuStorage::disk('qiniu');
            $NewAspic = 'store_master/'.md5(date('Y-m-d H:i:s').$clientName).".".$extension;
            $bool = $disk -> put($NewAspic,file_get_contents($file->getRealPath()));
            if($bool){
                //如果
                $res = ShopMaster::where('hmid',$id)->update(['privilege'=>$privilege,'nickname'=>$nickname,'headpic'=>$NewAspic,'referral'=>$referral,'subscribe'=>$subscribe,'ismanage'=>$ismanage,'asstatus'=>$asstatus]);
                if($res){
                    //则删掉原来的图片
                    $delete = $disk -> delete($headpic);
                    if($delete)
                    return back() -> with(['info'=>'数据更新成功']);
                    return back() -> with(['info'=>'数据更新成功,原头像没有删除,地址:'.$headpic]);
                }else{
                    return back() -> with(['info'=>'数据更新事变']);
                }
            }else{
                //图片存入七牛失败
                return back() -> with(['info'=>'头像存入七牛失败,请重新编辑']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $privilege = Input::get('ismanage');
        if($privilege == '1'){
            return Response() -> json(['status'=>'notallow','msg'=>'属于超管,不能删除']);
        }
        $headpic = ShopMaster::where('hmid',$id)->value('headpic');
        $res = ShopMaster::where('hmid',$id)->delete();
        if($res){
            $disk = QiniuStorage::disk('qiniu');
            $bool = $disk -> delete($headpic);
            if($bool){
                return Response() -> json(['status'=>'success','msg'=>'删除成功']);
            }else{
                return Response() -> json(['status'=>'error','msg'=>'删除七牛图片失败,地址:'.$headpic]);
            }
        }else{
            return Response() -> json(['status'=>'error','msg'=>'删除失败']);

        }

    }
    public function show_shopmaster($id)
    {
        //根据id获取信息
        $datas = ShopMaster::where('hmid',$id)->first();
        $shopname =  Shop::where('shopid',$datas['shopid'])->value('shopname');
        return view('admin.shopmaster.show',compact('datas','shopname'));
    }
}
